﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Waker.Common;

namespace Waker.SystemSetting
{
    public partial class FormUserEdit : Waker.Base.FormBasePop
    {

        private DataRow _DrSource;
        public FormUserEdit(DataRow dr)
        {
            InitializeComponent();
            _DrSource = dr;
            this.Load += new EventHandler(FormUserEdit_Load);
        }

        private void FormUserEdit_Load(object sender, EventArgs e)
        {
            this.Text += _DrSource == null ? "添加" : "修改";
            if (_DrSource != null)
            {
                //txtGroupName.Text = _DrSource["GroupName"] == DBNull.Value ? string.Empty : _DrSource["GroupName"].ToString();
                //txtDescription.Text = _DrSource["Description"] == DBNull.Value ? string.Empty : _DrSource["Description"].ToString(); 
                ControlUtil.BindData(this.Controls, _DrSource);

                if (_DrSource["Password"] != DBNull.Value)
                    txtPassword.Text = txtPassword1.Text = Util.DecryptDES(_DrSource["Password"].ToString());
                txtUserCode.Enabled = false;
            }
        }
        protected override void FormSave()
        {
            base.errorPop.ClearErrors();
            if (string.IsNullOrEmpty(txtUserCode.Text))
            {
                base.errorPop.SetError(txtUserCode, "用户名不能为空");
                return;
            }
            if (string.IsNullOrEmpty(txtRealName.Text))
            {
                base.errorPop.SetError(txtRealName, "姓名不能为空");
                return;
            }
            var password = string.Empty;

            //if (!string.IsNullOrEmpty(txtPassword.Text) || !string.IsNullOrEmpty(txtPassword1.Text))
            //{
            if (txtPassword.Text == txtPassword1.Text)
                password = Util.EncryptDES(txtPassword.Text);
            else
            {
                base.errorPop.SetError(txtPassword1, "两次密码不一致");
                return;
            }
            //}
            var isOk = new BAL.BAL_SystemSetting().SaveUser(
                _DrSource == null
                , txtUserCode.Text
                , txtRealName.Text
                , password
                , txtPhone.Text
                , txtQQ.Text
                , txtDescription.Text
                );
            if (isOk)
            {
                UI.ShowInfo("保存成功");
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                UI.ShowError("保存失败");
            }
        }
    }
}
